<template>
  <div class="one dp-flex dp-flex-jc-ac mt10">
    <el-button type="primary" @click="convertBtn">转换</el-button>
  </div>
  <div class="dp-flex" style="padding: 0 20px; box-sizing: border-box">
    <div class="dp-flex1">
      <h2>ts类</h2>
      <div class="dp-flex dp-flex-jc-ac">
        <el-input
          v-model="tstext"
          style="width: 90%"
          :rows="32"
          type="textarea"
          placeholder="ts原始类"
        />
      </div>
    </div>

    <div class="dp-flex1">
      <h2>中间</h2>
      <div class="dp-flex dp-flex-jc-ac">
        <el-input
          v-model="centext"
          style="width: 90%"
          :rows="32"
          type="textarea"
          placeholder="中间类"
        />
      </div>
    </div>

    <div class="dp-flex1">
      <h2>末尾</h2>
      <div class="dp-flex dp-flex-jc-ac">
        <el-input
          v-model="finallytext"
          style="width: 90%"
          :rows="32"
          type="textarea"
          placeholder="末尾类"
        />
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ElMessage } from "element-plus";
import { onMounted, ref } from "vue";
let tstext = ref<string>("");
let centext = ref<string>("");
let finallytext = ref<string>("");

/**类名*/
let className = ref<string>("");

onMounted(() => {
  init();
});
function init() {
  tstext.value =
    "export class Tpositiondevice {" +
    "\n" +
    "public editmode: string; // 编辑模式" +
    "\n" +
    "public fid: number; // 本身id" +
    "\n" +
    "public fmobiphone: string; //" +
    "\n" +
    "public fpass: string; //" +
    "\n" +
    "public fimei: string; //" +
    "\n" +
    "public fdeviceid: string; //" +
    "\n" +
    "public ftype: string; // 手机名等" +
    "\n" +
    "public fdevicetype: string; // android iphone" +
    "\n" +
    "public fname: string; //" +
    "\n" +
    "public sjson: string;" +
    "\n" +
    "public list: Workflowstep[];" +
    "\n" +
    "}";
}

function convertBtn() {
  centext.value = "";
  // console.error("tstext", tstext.value);
  let strarr = tstext.value.split("\n");
  console.log("strarr", strarr);
  let zongArr = [];
  let classNameShow = false;
  if (strarr.length > 0) {
    for (let i = 0; i < strarr.length; i++) {
      const it = strarr[i];
      if (!classNameShow) {
        //判断类名
        let regex = /(?:export\s+)?class\s+([\w$]+)\s*\{\s*$/;
        let match = it.match(regex);

        let className = "";
        if (match) {
          let s = match[1];
          className = "class  " + s + " { \n";
        }
        zongArr.push(className);
        classNameShow = true;
      }

      let str = convertTypeAndList(it);
      if (str) {
        let sstr = str + "\n";
        zongArr.push(sstr);
      }

      console.error("output");
    }
    if (zongArr.length > 0) {
      for (let i = 0; i < zongArr.length; i++) {
        const it = zongArr[i];
        className.value += it;
      }
    }
  }
}
function convertTypeAndList(text) {
  // 将 "public fid: number;" 转换为 "int? fid;"
  text = text.replace(/public (\w+): number;/g, "int? $1;");

  // 将 "public fid: string;" 转换为 "String? fid;"
  text = text.replace(/public (\w+): string;/g, "String? $1;");

  // 将 "public lists: xx[]" 转换为 "List<xx>? lists;"
  text = text.replace(/public (\w+): (\w+)\[\];/g, "List<$2>? $1;");

  return text;
}
// class ApiCarperson {
//   int? code;
//   String? msg;
//   Cardatacontent? data;

//   ApiCarperson();

//   ApiCarperson.fromJson(Map<String, dynamic> json) {
//     code = json['code'];
//     msg = json['msg'];
//     if (json['data'] != null) {
//       var obj = Cardatacontent.fromJson(json['data']);
//       data = obj;
//     } else {
//       null;
//     }
//   }

//   Map<String, dynamic> toJson() {
//     final Map<String, dynamic> data = new Map<String, dynamic>();
//     data['code'] = this.code;
//     data['msg'] = this.msg;
//     data['data'] = this.data;
//     return data;
//   }
// }
</script>
<style lang="scss" scoped>
.one {
  margin-bottom: 130px;
}

.dp-flex1 {
  flex: 1;
}
</style>
